//
//  SEGImageMaskView.m
//  ExcellentHome
//
//  Created by 揭婧雯 on 16/11/23.
//  Copyright © 2016年 陈区. All rights reserved.
//

#import "SEGImageMaskView.h"
#import <UIKit/UIKit.h>

@interface SEGImageMaskView ()
@property (nonatomic) CGContextRef imageContext;
@property (nonatomic) CGColorSpaceRef colorSpace;
@property (nonatomic) float scaleOfBrash;
@property (nonatomic, readwrite) BOOL canShowResult;
@property (nonatomic, readwrite) double percentsOfImageMasked;
- (UIImage*)handleTouches:(NSSet*)touches;
@end
@implementation SEGImageMaskView
@synthesize imageContext;
@synthesize colorSpace;
@synthesize percentsOfImageMasked;
@synthesize scaleOfBrash = _scaleOfBrash;

#pragma mark - Initinal
- (id)initWithFrame:(CGRect)frame image:(UIImage*)img
{
    self = [super initWithFrame:frame];
    if (self) { // Initialization code
        self.userInteractionEnabled = YES;
        self.backgroundColor = [UIColor clearColor];
        self.image = img;
        //        self.percentsOfImageMasked = 0.0;
        self.canShowResult = NO;
        self.colorSpace = CGColorSpaceCreateDeviceRGB();
        self.imageContext = CGBitmapContextCreate(0, self.bounds.size.width, self.bounds.size.height, 8, self.bounds.size.width * 4, self.colorSpace, kCGImageAlphaPremultipliedLast);
        CGContextDrawImage(self.imageContext, CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height), self.image.CGImage);
        CGContextSetBlendMode(self.imageContext, kCGBlendModeClear);
    }
    return self;
}
#define DEFAULT_SCALE_OF_BRASH 20.0
- (float)scaleOfBrash
{
    if (!_scaleOfBrash)
        _scaleOfBrash = DEFAULT_SCALE_OF_BRASH;
    return _scaleOfBrash;
}
#pragma mark - UIResponder
- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
    self.image = [self handleTouches:touches];
}
- (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event
{
    self.image = [self handleTouches:touches];
}
- (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event
{
    self.image = [self handleTouches:touches];
}
#pragma mark -
- (UIImage*)handleTouches:(NSSet*)touches
{
    CGContextSetFillColorWithColor(self.imageContext, [UIColor clearColor].CGColor);
    CGContextSetStrokeColorWithColor(self.imageContext, [UIColor colorWithRed:0 green:0 blue:0 alpha:0].CGColor); //process Touches
    for (UITouch* touch in touches) {
        CGContextBeginPath(self.imageContext);
        CGPoint touchPoint = [touch locationInView:self];
        touchPoint.y = self.bounds.size.height - touchPoint.y;
        CGRect rect = CGRectMake(touchPoint.x - self.scaleOfBrash / 2.0, touchPoint.y - self.scaleOfBrash / 2.0, self.scaleOfBrash, self.scaleOfBrash);
        if (UITouchPhaseBegan == touch.phase) { //on began, we just draw a ellipse
            CGContextAddEllipseInRect(self.imageContext, rect);
            CGContextFillPath(self.imageContext);
            CGContextStrokePath(self.imageContext);
            if (CGRectContainsPoint(CGRectMake(self.bounds.origin.x + self.bounds.size.width / 2.0 - 10, self.bounds.origin.y + self.bounds.size.height / 2.0 - 10, 20, 20), touchPoint)) {
                self.canShowResult = YES;
            }
        }
        else if (UITouchPhaseMoved == touch.phase) { //then touch moved, we draw superior-width line
            CGPoint prevPoint = [touch previousLocationInView:self];
            prevPoint.y = self.bounds.size.height - prevPoint.y;
            CGContextSetStrokeColor(self.imageContext, CGColorGetComponents([UIColor yellowColor].CGColor));
            CGContextSetLineCap(self.imageContext, kCGLineCapRound);
            CGContextSetLineWidth(self.imageContext, self.scaleOfBrash);
            CGContextMoveToPoint(self.imageContext, prevPoint.x, prevPoint.y);
            CGContextAddLineToPoint(self.imageContext, rect.origin.x + rect.size.width / 2.0, rect.origin.y + rect.size.height / 2.0);
            CGContextStrokePath(self.imageContext);
            if (CGRectContainsPoint(CGRectMake(self.bounds.origin.x + self.bounds.size.width / 2.0 - 10, self.bounds.origin.y + self.bounds.size.height / 2.0 - 10, 20, 20), prevPoint)) {
                self.canShowResult = YES;
            }
        }
        else if (UITouchPhaseEnded == touch.phase) {
            if (self.canShowResult) {
                [self showResult];
                return nil;
            }
        }
    }
    CGImageRef cgImage = CGBitmapContextCreateImage(self.imageContext);
    UIImage* image = [UIImage imageWithCGImage:cgImage];
    CGImageRelease(cgImage);
    return image;
}
- (void)showResult
{
    //结果显示后不再可点
    self.userInteractionEnabled = NO;
    self.completion();
}
@end
